package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.enums.eWhiteIpType;
import com.yanqu.road.entity.servercenter.IpInfo;
import com.yanqu.road.entity.servercenter.whitelist.WhiteIP;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IpInfoDaoImpl extends TempDao {

	public void getWhiteIpList(List<String> playerWhiteIpList, List<String> consoleWhiteIpList, List<String> tempBlackList, List<String> tempAdminList) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_white_ip";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				String content = rs.getString("content");
				if(null != content) {
					content = content.trim();
					int type = rs.getInt("type");
					if (eWhiteIpType.WhiteIpPlayer.getValue() == type) {
						playerWhiteIpList.add(content);
					}else if (eWhiteIpType.WhiteIpConsole.getValue() == type){
						consoleWhiteIpList.add(content);
					} else if (eWhiteIpType.BlackIpConsole.getValue() == type) {
						tempBlackList.add(content);
					}else if (eWhiteIpType.WhiteIpAdmin.getValue() == type){
						tempAdminList.add(content);
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
	}

//	public Map<String, IpInfo> getIpInfo() {
//		ResultSet rs = null;
//		CallableStatement call = null;
//		Map<String, IpInfo> ipMap = new HashMap<>();
//		String sql = "select country,city,county,province,ip,cityId from t_s_ip";
//		try {
//			getDb().setConnName(DbNameStrategy.getServerDb());
//
//			call = getDb().getConnection().prepareCall(sql);
//			rs = call.executeQuery();
//			while (rs.next()) {
//				IpInfo ipInfo = new IpInfo();
//				ipInfo.setCountry(rs.getString("country"));
//				ipInfo.setCity(rs.getString("city"));
//				ipInfo.setCounty(rs.getString("county"));
//				ipInfo.setProvince(rs.getString("province"));
//				ipInfo.setCityId(rs.getInt("cityId"));
//				ipMap.put(rs.getString("ip"), ipInfo);
//			}
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		} finally {
//			getDb().closeConnection(call, rs);
//		}
//		return ipMap;
//	}

	public Map<String, IpInfo> getIpInfo() {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<String, IpInfo> dataMap = new HashMap<>();
		String sql = "select country,region,city,ip from t_s_ip2city";
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());

			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				IpInfo ipInfo = new IpInfo();
				ipInfo.setIp(rs.getString("ip"));
				ipInfo.setCountry(rs.getString("country"));
				ipInfo.setProvince(rs.getString("region"));
				ipInfo.setCity(rs.getString("city"));
				dataMap.put(ipInfo.getIp(), ipInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public List<WhiteIP> getWhiteIPs() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_white_ip";
		List<WhiteIP> res = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String content = rs.getString("content");
				String remark = rs.getString("remark");
				int type = rs.getInt("type");
				res.add(new WhiteIP(id, content, remark, type));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return res;
	}
}
